package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class SaleCtr here.
 * 
 * @author (Gruppe 2) 
 * @version (a version number or a date)
 */
public class SaleCtr
{
    private SaleCon saleCon;
    private ItemCon itemCon;
    private CustomerCon customerCon;

    public SaleCtr()
    {
        saleCon = saleCon.getInstance();
        customerCon = customerCon.getInstance();
        itemCon = itemCon.getInstance();
    }
    
    public void createCustomerSale(int id, int itemId, int customerId, String regDate, String status, int quantity)
    {
        Sale newSale = null;
        Partsale newPartsale = null;
        boolean itemFound = false;
        boolean customerFound = false;
        if(saleCon.getSale(id) == null)
        {
        	newSale = new Sale(id, regDate, status);
        	if(itemCon.getItem(itemId) != null)
            {
                newPartsale = new Partsale(quantity, itemCon.getItem(itemId));
                itemFound = true;
            }
            else
            {
                System.out.println("Matching item not found for id: "+itemId);
            }
            if(customerCon.getCustomer(customerId) != null)
            {
                newSale.setCustomer(customerCon.getCustomer(customerId));
                customerFound = true;
            }
            else
            {
                System.out.println("Matching customer not found for id: "+customerId);
            }
        }
        else
        {
            System.out.println("There already exists a sale with id: "+id+" in the system");
        }
        if(itemFound && customerFound)
        {
            newSale.addPartsale(newPartsale);
            saleCon.addSale(newSale);
            saleCon.getSale(id).calcTotalPrice();
        }
        else
        {
            System.out.println("Mathcing item and/or customer was not found in the system");
        }
    }
    
    public void createBusinessSale(int id, int itemId, int businessId, String regDate, String status, int quantity)
    {
        Sale newSale = null;
        Partsale newPartsale = null;
        boolean itemFound = false;
        boolean customerFound = false;
        if(saleCon.getSale(id) == null)
        {
            newSale = new Sale(id, regDate, status);
            if(itemCon.getItem(itemId) != null)
            {
                newPartsale = new Partsale(quantity, itemCon.getItem(itemId));
                itemFound = true;
            }
            else
            {
                System.out.println("Matching item not found for id: "+itemId);
            }
            if(customerCon.getBusiness(businessId) != null)
            {
                newSale.setBusiness(customerCon.getBusiness(businessId));
                customerFound = true;
            }
            else
            {
                System.out.println("Matching business customer not found for id: "+businessId);
            }
        }
        else
        {
            System.out.println("There already exists a sale with id: "+id+" in the system");
        }
        if(itemFound && customerFound)
        {
        	newSale.addPartsale(newPartsale);
        	saleCon.addSale(newSale);
        	saleCon.getSale(id).calcTotalPrice();
        }
        else
        {
        	System.out.println("Mathcing item(s) and/or customer(s) have not been found in the system");
        }
    }
    
    public void addItemToSale(int id, int itemId, int quantity)
    {
    	if(saleCon.getSale(id) != null)
    	{
    		if(itemCon.getItem(itemId) != null)
    		{
    			int i = 0;
    			boolean itemFound = false;
    			while(!itemFound && i < saleCon.getSale(id).getPartsale().size())
    			{
    				if(saleCon.getSale(id).getPartsale().get(i).getItem().getId() == itemId)
    				{
    					itemFound = true;
    				}
    				else
    				{
    					i++;
    				}
    			}
    			if(itemFound)
    			{
    				int q = saleCon.getSale(id).getPartsale().get(i).getQuantity() + quantity;
    				saleCon.getSale(id).getPartsale().get(i).setQuantity(q);
    			}
    			else
    			{
    				Partsale newPartsale = new Partsale(quantity, itemCon.getItem(itemId));
    				saleCon.getSale(id).addPartsale(newPartsale);
    			}
    		}
    		else
    		{
    			System.out.println("Matching item not found for id: "+itemId);
    		}
    		saleCon.getSale(id).calcTotalPrice();
    	}
    	else
    	{
    		System.out.println("Matching sale not found for id: "+id);
    	}
    }
    
    public Sale getSaleById(int id)
    {
        return saleCon.getSale(id);
    }
    
    public void printSale(int id)
    {
        saleCon.printSale(id);
    }
    
    public void printSaleList()
    {
        saleCon.printSaleList();
    }
    
    public void removeSale(int id)
    {
        saleCon.removeSale(id);
    }
}
